using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;

using EntLibEC.Cmn;
using EntLibEC.Info.Online;

namespace EntLibEC.Dac.Online
{
    public class NewsDac
    {
        public NewsDac()
		{

		}

        public int Insert(NewsInfo oParam)
        {
            string sql = @"INSERT INTO Ent_News
                            (
                            Title, LinkUrl, ExpireDate, TopMost, CreateUserSysNo, CreateDate, Content, NewsType
                            )
                            VALUES (
                            @Title, @LinkUrl, @ExpireDate, @TopMost, @CreateUserSysNo, @CreateDate, @Content, @NewsType
                            );set @SysNo = SCOPE_IDENTITY();";
            SqlCommand cmd = new SqlCommand(sql);

            SqlParameter paramSysNo = new SqlParameter("@SysNo", SqlDbType.Int, 4);
            SqlParameter paramTitle = new SqlParameter("@Title", SqlDbType.NVarChar, 200);
            SqlParameter paramLinkUrl = new SqlParameter("@LinkUrl", SqlDbType.NVarChar, 200);
            SqlParameter paramExpireDate = new SqlParameter("@ExpireDate", SqlDbType.DateTime);
            SqlParameter paramTopMost = new SqlParameter("@TopMost", SqlDbType.Int, 4);
            SqlParameter paramCreateUserSysNo = new SqlParameter("@CreateUserSysNo", SqlDbType.Int, 4);
            SqlParameter paramCreateDate = new SqlParameter("@CreateDate", SqlDbType.DateTime);
            SqlParameter paramContent = new SqlParameter("@Content", SqlDbType.NText);
            SqlParameter paramNewsType = new SqlParameter("@NewsType", SqlDbType.Int, 4);

            paramSysNo.Direction = ParameterDirection.Output;

            if (oParam.Title != AppConst.StringNull)
                paramTitle.Value = oParam.Title;
            else
                paramTitle.Value = System.DBNull.Value;
            if (oParam.LinkUrl != AppConst.StringNull)
                paramLinkUrl.Value = oParam.LinkUrl;
            else
                paramLinkUrl.Value = System.DBNull.Value;
            if (oParam.ExpireDate != AppConst.DateTimeNull)
                paramExpireDate.Value = oParam.ExpireDate;
            else
                paramExpireDate.Value = System.DBNull.Value;
            if (oParam.TopMost != AppConst.IntNull)
                paramTopMost.Value = oParam.TopMost;
            else
                paramTopMost.Value = System.DBNull.Value;
            if (oParam.CreateUserSysNo != AppConst.IntNull)
                paramCreateUserSysNo.Value = oParam.CreateUserSysNo;
            else
                paramCreateUserSysNo.Value = System.DBNull.Value;
            if (oParam.CreateDate != AppConst.DateTimeNull)
                paramCreateDate.Value = oParam.CreateDate;
            else
                paramCreateDate.Value = System.DBNull.Value;
            if (oParam.Content != AppConst.StringNull)
                paramContent.Value = oParam.Content;
            else
                paramContent.Value = System.DBNull.Value;
            if (oParam.NewsType != AppConst.IntNull)
                paramNewsType.Value = oParam.NewsType;
            else
                paramNewsType.Value = System.DBNull.Value;



            cmd.Parameters.Add(paramSysNo);
            cmd.Parameters.Add(paramTitle);
            cmd.Parameters.Add(paramLinkUrl);
            cmd.Parameters.Add(paramExpireDate);
            cmd.Parameters.Add(paramTopMost);
            cmd.Parameters.Add(paramCreateUserSysNo);
            cmd.Parameters.Add(paramCreateDate);
            cmd.Parameters.Add(paramContent);
            cmd.Parameters.Add(paramNewsType);

            return SqlHelper.ExecuteNonQuery(cmd, out oParam.SysNo);
        }


        public int Update(NewsInfo oParam)
        {
            string sql = @"UPDATE Ent_News SET 
                           Title=@Title, LinkUrl=@LinkUrl, ExpireDate=@ExpireDate, TopMost=@TopMost, 
                           CreateUserSysNo=@CreateUserSysNo, CreateDate=@CreateDate, Content=@Content, NewsType=@NewsType
                           WHERE SysNo=@SysNo";
            SqlCommand cmd = new SqlCommand(sql);

            SqlParameter paramSysNo = new SqlParameter("@SysNo", SqlDbType.Int, 4);
            SqlParameter paramTitle = new SqlParameter("@Title", SqlDbType.NVarChar, 200);
            SqlParameter paramLinkUrl = new SqlParameter("@LinkUrl", SqlDbType.NVarChar, 200);
            SqlParameter paramExpireDate = new SqlParameter("@ExpireDate", SqlDbType.DateTime);
            SqlParameter paramTopMost = new SqlParameter("@TopMost", SqlDbType.Int, 4);
            SqlParameter paramCreateUserSysNo = new SqlParameter("@CreateUserSysNo", SqlDbType.Int, 4);
            SqlParameter paramCreateDate = new SqlParameter("@CreateDate", SqlDbType.DateTime);
            SqlParameter paramContent = new SqlParameter("@Content", SqlDbType.NText);
            SqlParameter paramNewsType = new SqlParameter("@NewsType", SqlDbType.Int, 4);

            if (oParam.SysNo != AppConst.IntNull)
                paramSysNo.Value = oParam.SysNo;
            else
                paramSysNo.Value = System.DBNull.Value;            
            if (oParam.Title != AppConst.StringNull)
                paramTitle.Value = oParam.Title;
            else
                paramTitle.Value = System.DBNull.Value;
            if (oParam.LinkUrl != AppConst.StringNull)
                paramLinkUrl.Value = oParam.LinkUrl;
            else
                paramLinkUrl.Value = System.DBNull.Value;
            if (oParam.ExpireDate != AppConst.DateTimeNull)
                paramExpireDate.Value = oParam.ExpireDate;
            else
                paramExpireDate.Value = System.DBNull.Value;
            if (oParam.TopMost != AppConst.IntNull)
                paramTopMost.Value = oParam.TopMost;
            else
                paramTopMost.Value = System.DBNull.Value;
            if (oParam.CreateUserSysNo != AppConst.IntNull)
                paramCreateUserSysNo.Value = oParam.CreateUserSysNo;
            else
                paramCreateUserSysNo.Value = System.DBNull.Value;
            if (oParam.CreateDate != AppConst.DateTimeNull)
                paramCreateDate.Value = oParam.CreateDate;
            else
                paramCreateDate.Value = System.DBNull.Value;
            if (oParam.Content != AppConst.StringNull)
                paramContent.Value = oParam.Content;
            else
                paramContent.Value = System.DBNull.Value;
            if (oParam.NewsType != AppConst.IntNull)
                paramNewsType.Value = oParam.NewsType;
            else
                paramNewsType.Value = System.DBNull.Value;


            cmd.Parameters.Add(paramSysNo);
            cmd.Parameters.Add(paramTitle);
            cmd.Parameters.Add(paramLinkUrl);
            cmd.Parameters.Add(paramExpireDate);
            cmd.Parameters.Add(paramTopMost);
            cmd.Parameters.Add(paramCreateUserSysNo);
            cmd.Parameters.Add(paramCreateDate);
            cmd.Parameters.Add(paramContent);
            cmd.Parameters.Add(paramNewsType);

            return SqlHelper.ExecuteNonQuery(cmd);
        }


        public int Delete(int SysNo)
        {
            string sql = "delete from Ent_News where sysno = " + SysNo;
            SqlCommand cmd = new SqlCommand(sql);
            return SqlHelper.ExecuteNonQuery(cmd);
        }


    }
}
